from rest_framework.viewsets import ModelViewSet
from apps.goods.models import SKUImage
from apps.meiduo_admin.serializers.images import ImageModelSerializer
from apps.meiduo_admin.utils import PageNum
from rest_framework.response import Response
from fdfs_client.client import Fdfs_client


class ImageModelViewSet(ModelViewSet):

    # 查询结果集
    queryset = SKUImage.objects.all()
    # 序列化器
    serializer_class = ImageModelSerializer
    #分页
    pagination_class = PageNum

    def create(self, request, *args, **kwargs):
        """
        1.接收数据
        2.验证数据
        3.Fdfs图片保存
            3.1    加载Fdfs的配置文件,来设置Tracker Server.同事创建客户
            3.2     上传图片.二进制数据
            3.3     根据结果进行判断
        4.数据入库
        5.返回响应
        """
        # 1.接收数据
        data = request.data
        """
        表单提交数据:
        "sku": "SKU商品id",
        "image": "SKU商品图片"
        """
        # 2.验证数据
        sku_id = data.get('sku')
        try:
            sku = SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            return Response({'msg': '没有此商品'})
        # 3.Fdfs图片保存
        from fdfs_client.client import Fdfs_client
        #     3.1    加载Fdfs的配置文件,来设置Tracker Server.同事创建客户
        client = Fdfs_client('meiduo_mall/utils/fastdfs/client.conf')
        #     3.2     上传图片.二进制数据
        # 上传的图片我们要读取图片二进制
        # 获取上传的图片对象
        image = data.get('image')
        # 读取二进制  read() 方法是读取图片的二进制数据
        image_data = image.read()
        # 上传图片.二进制数据.上传之后有返回结果
        result = client.upload_by_buffer(image_data)
        #     3.3     根据结果进行判断
        """
        {
            'Remote file_id': 'group1/M00/00/00/wKjlhFsTgJ2AJvG_AAAyZgOTZN0850.jpg', 
            'Uploaded size': '12.00KB',
             'Local file name': '/home/python/Desktop/images/0.jpg',
              'Storage IP': '192.168.229.132',
              'Group name': 'group1', 
              'Status': 'Upload successed.'
        }
        """
        if result.get('Status') != 'Upload successed.':
            return Response({'msg': '请稍后再试'})
        file_id = result.get('Remote file_id')

        # 4.数据入库
        new_image = SKUImage.objects.create(
            sku=sku,
            image=file_id
        )
        # 5.返回响应
        # 响应状态码 必须是 201
        # 因为前端是根据 201 来进行判断的

        return Response({
            "id": new_image.id,
            "sku": sku_id,
            "image": file_id
        }, status=201)

    def update(self, request, *args, **kwargs):
        # 获取更新对象
        old_skuimage = self.get_object()
        # 接收数据
        # 验证数据
        image = request.data.get('image')
        image_data = image.read()

        # 通过fastdfs保存图片
        # 创建fastdfs客户端,加载配置文件
        client = Fdfs_client('meiduo_mall/utils/fastdfs/client.conf')
        # 上传
        result = client.upload_by_buffer(image_data)

        # 根据上传结果判断
        if result['Status'] != 'Upload successed.':
            return Response({'msg': '稍后再试'})
        file_id = result.get('Remote file_id')

        # 更新数据
        old_skuimage.image = file_id
        old_skuimage.save()

        # 返回
        return Response({
            'id': old_skuimage.id,
            'image': file_id,
            'sku': request.data.get('sku')
        })




# class ImageModelViewSet(ModelViewSet):
#     queryset = SKUImage.objects.all()
#     # 序列化器
#     serializer_class = ImageModelSerializer
#     #分页
#     pagination_class = PageNum
#
#     def create(self, request, *args, **kwargs):
#         # 1.接收数据
#         data = request.data
#         # 2.验证数据
#         sku_id = data.get('sku')
#         try:
#             sku = SKU.objects.get(pk=sku_id)
#         except SKU.DoesNotExist:
#             return Response({'msg': '没有此商品'})
#         #     3.1    加载Fdfs的配置文件,来设置Tracker Server.同事创建客户
#         client = Fdfs_client('meiduo_mall/utils/fastdfs/client.conf')
#         #     3.2     上传图片.二进制数据
#         # 上传的图片我们要读取图片二进制
#         # 获取上传的图片对象
#         image = data.get('image')
#         # 读取二进制  read() 方法是读取图片的二进制数据
#         image_data = image.read()
#         # 上传图片.二进制数据.上传之后有返回结果
#         result = client.upload_by_buffer(image_data)
#
#         if result.get('Status') != 'Upload successed.':
#             return Response({'msg': '请稍后再试'})
#         file_id = result.get('Remote file_id')
#
#         # 4.数据入库
#         new_image = SKUImage.objects.create(
#             sku=sku,
#             image=file_id
#         )
#
#         return Response({
#             "id": new_image.id,
#             "sku": sku_id,
#             "image": file_id
#         }, status=201)

    # def update(self, request, *args, **kwargs):
    #     # 获取更新对象
    #     old_skuimage = self.get_object()
    #     # 接收数据
    #     # 验证数据
    #     image = request.data.get('image')
    #     image_data = image.read()
    #     # 通过fastdfs保存图片
    #     # 创建fastdfs客户端,加载配置文件
    #     client = Fdfs_client('meiduo_mall/utils/fastdfs/client.conf')
    #     # 上传
    #     result = client.upload_by_buffer(image_data)
    #     # 根据上传结果判断
    #     if result['Status'] != 'Upload successed.':
    #         return Response({'msg': '稍后再试'})
    #     file_id = result.get('Remote file_id')
    #     # 更新数据
    #     old_skuimage.image = file_id
    #     old_skuimage.save()
    #
    #     # 返回
    #     return Response({
    #         'id': old_skuimage.id,
    #         'image': file_id,
    #         'sku': request.data.get('sku')
    #     })




from rest_framework.generics import ListAPIView
from apps.goods.models import SKU
from apps.meiduo_admin.serializers.images import SKUSimpleSerializer
"""
如果我们想要实现图片的上传
1. 必须先展示SKU列表.因为我们上传图片的时候 必须要给某一个SKU上传图片
2. 通过Fdfs上传图片
"""
# 获取sku列表的视图


class SKUSimpleListAPIView(ListAPIView):

    queryset = SKU.objects.all()

    serializer_class = SKUSimpleSerializer
